package xinya.com.baselibrary.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import xinya.com.baselibrary.constant.Constant;
import xinya.com.baselibrary.eventbus.EventBusMsg;
import xinya.com.baselibrary.router.RouterUrl;
import xinya.com.baselibrary.router.RouterUtil;
import xinya.com.baselibrary.utils.LogUtil;


public class MyServiceX extends Service {
    //自定义U盘读写权限
    private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    /**
     * Binder实现了IBinder接口
     */
    public class MyBind extends Binder {

    }


    @Override
    public IBinder onBind(Intent intent) {
        return new MyBind();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
        registerUDiskReceiver();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        Intent intent = new Intent();
        intent.setClass(this, MyServiceX.class);
        startService(intent);
        if (mOtgReceiver!=null){
            unregisterReceiver(mOtgReceiver);
        }
    }

    /**
     * @description OTG广播注册
     * @author ldm
     * @time 2017/9/1 17:19
     */
    private void registerUDiskReceiver() {
        //监听otg插入 拔出
        IntentFilter usbDeviceStateFilter = new IntentFilter();
        usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        registerReceiver(mOtgReceiver, usbDeviceStateFilter);
        //注册监听自定义广播
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        registerReceiver(mOtgReceiver, filter);
    }

    /**
     * @description OTG广播，监听U盘的插入及拔出
     * @author ldm
     * @time 2017/9/1 17:20
     * @param
     */
    private BroadcastReceiver mOtgReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTION_USB_PERMISSION://接受到自定义广播
                    UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    //允许权限申请
                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if (usbDevice != null) {
                            //用户已授权，可以进行读取操作
//                            readDevice(getUsbMass(usbDevice));
                        } else {
//                            showToastMsg("没有插入U盘");
                        }
                    } else {
//                        showToastMsg("未获取到U盘权限");
                    }
                    break;
                case UsbManager.ACTION_USB_DEVICE_ATTACHED://接收到U盘设备插入广播
                    UsbDevice device_add = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    if (device_add != null) {
                        //接收到U盘插入广播，尝试读取U盘设备数据
//                        redUDiskDevsList();
                    }
                    break;
                case UsbManager.ACTION_USB_DEVICE_DETACHED://接收到U盘设设备拔出广播
//                    showToastMsg("U盘已拔出");
                    break;
            }
        }
    };

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventReceiver(EventBusMsg eventBusMsg) {
        switch (eventBusMsg.key) {
            case Constant.EventKey.keepLive:
                LogUtil.LogShitou("MyService--onEventReceiver", "111111111");
                new Handler().postDelayed(() -> {
                    LogUtil.LogShitou("MyService--onEventReceiver", ""+Constant.keepLive);
                    if (Constant.keepLive) {
                        RouterUtil.getPostcardWithTransition(MyServiceX.this, RouterUrl.Main.main_home)
                                .navigation();
                    }
                },10000);
                break;
            default:
                break;
        }
    }
}